package com.loan.web.api;

import com.loan.entity.Product;
import com.loan.entity.ResponseResult;
import com.loan.exception.ProductException;
import com.loan.service.ProductService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author fengbenpeng
 * @Desctiption 产品控制器
 * @date 2022/1/17 19:12
 */
@Slf4j
@RestController
@RequestMapping("/api/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    /**
     * 增加产品
     * @param product
     * @return
     */
    @RequestMapping("/add")
    public ResponseResult add(@RequestBody Product product) {
        log.info("增加产品");
        log.info("参数:" + product);

        if (product.getTime() < 0 || product.getMaxMoney().compareTo(BigDecimal.ZERO)<=0 || product.getRate().compareTo(BigDecimal.ZERO)<=0) {
             return new ResponseResult(400,"利率,金额,及还款时间都不能为负数");
        }
        if (product.getName().length() < 1) {
            return new ResponseResult(400, "产品的名字不能为空");
        } else {
            productService.addProduct(product);
            return new ResponseResult(200, "添加成功");
        }
    }

    /**
     * 根据id删除产品
     * @param id
     * @return
     */
    @RequestMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable int id) {
        productService.deleteProduct(id);
        return new ResponseResult(200, "删除成功");
    }

    /**
     * 修改更新产品
     * @param product
     * @return
     */
    @RequestMapping("/update")
    public ResponseResult update(@RequestBody Product product) {
        productService.updateProduct(product);
        return new ResponseResult(200, "成功");
    }

    /**
     * 查询所有的产品信息
     */
    @RequestMapping("/getAll")
    @ResponseBody
    public ResponseResult<List<Product>> getProductAll() {
        List<Product> list = productService.list();
        return new ResponseResult<>(list);
    }
}
